package com.cz.kingdee.prod;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.cz.kingdee.KingdeeClient;
import com.cz.kingdee.dto.PrdMorpt;
import com.kingdee.bos.webapi.entity.*;
import com.kingdee.bos.webapi.sdk.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 根据汇报单号 删除生产汇报单
 */
public class DelPrdMorpt {
    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) throws Exception {
        KingdeeClient client = new KingdeeClient();
        K3CloudApi k3CloudApi = client.getProdKingdeeClient();
        QueryParam queryParam = new QueryParam();
        queryParam.setFormId("PRD_MORPT");
        queryParam.setFieldKeys("FID,FBillNo,FCreateDate,FDocumentStatus");

        String billNo = "SCHB00167177";
        //AND FDocumentStatus='C'
         //queryParam.setFilterString("FDate>='2025-01-01' AND FDate<='2025-01-31' AND FCreatorId='28859605' AND FWorkshipId='2539006'");
        //queryParam.setFilterString("FBillNo='SCHB00159952'");
        //queryParam.setStartRow(0);
        //queryParam.setLimit(10000);
        queryParam.setFilterString("FBillNo='"+billNo+"'");

        List<PrdMorpt> result = k3CloudApi.executeBillQuery(queryParam, PrdMorpt.class);
        System.out.println("查询结果集大小："+result.size());
        processPage(result, k3CloudApi);
        //result.forEach(System.out::println);
        // process(result,k3CloudApi);


    }

    public static void processPage(List<PrdMorpt> result, K3CloudApi k3CloudApi) {
        int batchSize = 50; // 每批数据的大小

        for (int i = 0; i < result.size(); i += batchSize) {
            int end = Math.min(i + batchSize, result.size());
            List<PrdMorpt> batch = result.subList(i, end);


            //System.out.println(count.incrementAndGet());
            List<String> numbers = batch.stream().map(PrdMorpt::getFBillNo).collect(Collectors.toList());

            OperateParam operateParam = new OperateParam();
            operateParam.setNumbers(numbers);
            OperatorResult operatorResult = null;
            try {
                operatorResult = k3CloudApi.unAudit("PRD_MORPT", operateParam);
            } catch (Exception e) {
                e.printStackTrace();
            }

            RepoResult repoResult = operatorResult.getResult();
            RepoStatus repoStatus = repoResult.getResponseStatus();
            List<SuccessEntity> successEntityList = repoStatus.getSuccessEntitys();
            List<RepoError> errorList = repoStatus.getErrors();

            if (CollUtil.isNotEmpty(successEntityList)) {
                System.out.println("成功:" + "################" + JSONUtil.toJsonStr(successEntityList));
            }

            if (CollUtil.isNotEmpty(errorList)) {
                System.out.println("失败:" + "------------------" + JSONUtil.toJsonStr(errorList));
            }


            try {
                operatorResult = k3CloudApi.delete("PRD_MORPT", operateParam);
            } catch (Exception e) {
                e.printStackTrace();
            }

            repoResult = operatorResult.getResult();
            repoStatus = repoResult.getResponseStatus();
            successEntityList = repoStatus.getSuccessEntitys();
            errorList = repoStatus.getErrors();

            if (CollUtil.isNotEmpty(successEntityList)) {
                System.out.println("成功:" + "################" + JSONUtil.toJsonStr(successEntityList));
            }

            if (CollUtil.isNotEmpty(errorList)) {
                System.out.println("失败:" + "------------------" + JSONUtil.toJsonStr(errorList));
            }
        }
    }


    public static void process(List<PrdMorpt> result, K3CloudApi k3CloudApi) {
        int batchSize = 10; // 每批数据的大小
        int numberOfThreads = 4; // 线程数量

        ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);

        for (int i = 0; i < result.size(); i += batchSize) {
            int end = Math.min(i + batchSize, result.size());
            List<PrdMorpt> batch = result.subList(i, end);

            executorService.submit(() -> {
                for (PrdMorpt prdMorpt : batch) {
                    //System.out.println(count.incrementAndGet());
                    List<String> numbers = new ArrayList<>();
                    numbers.add(prdMorpt.getFBillNo());

                    OperateParam operateParam = new OperateParam();
                    operateParam.setNumbers(numbers);
                    OperatorResult operatorResult = null;
                    try {
                        operatorResult = k3CloudApi.unAudit("PRD_MORPT", operateParam);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    RepoResult repoResult = operatorResult.getResult();
                    RepoStatus repoStatus = repoResult.getResponseStatus();
                    List<SuccessEntity> successEntityList = repoStatus.getSuccessEntitys();
                    List<RepoError> errorList = repoStatus.getErrors();

                    if (CollUtil.isNotEmpty(successEntityList)) {
                        System.out.println("成功:" + prdMorpt.getFBillNo() + "################" + JSONUtil.toJsonStr(successEntityList));
                    }

                    if (CollUtil.isNotEmpty(errorList)) {
                        System.out.println("失败:" + prdMorpt.getFBillNo() + "------------------" + JSONUtil.toJsonStr(errorList));
                    }


                    try {
                        operatorResult = k3CloudApi.delete("PRD_MORPT", operateParam);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    repoResult = operatorResult.getResult();
                    repoStatus = repoResult.getResponseStatus();
                    successEntityList = repoStatus.getSuccessEntitys();
                    errorList = repoStatus.getErrors();

                    if (CollUtil.isNotEmpty(successEntityList)) {
                        System.out.println("成功:" + prdMorpt.getFBillNo() + "################" + JSONUtil.toJsonStr(successEntityList));
                    }

                    if (CollUtil.isNotEmpty(errorList)) {
                        System.out.println("失败:" + prdMorpt.getFBillNo() + "------------------" + JSONUtil.toJsonStr(errorList));
                    }
                }
            });
        }

        executorService.shutdown();
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

